import datetime
import time


def transform_json(sourcefilepath, destfilepath):
    """
    :param sourcefilepath: 原 json 文件绝对路径
    :param destfilepath: 转换后存储新 json 文件绝对的路径
    :return: 
    原json 文件格式：
    [{"UserID":"A2WOH395IHGS0T",
    "Rating":5.0,"ReviewTime":1496177056,
    "Review":"风味独特，真的不错！",
    "MealID":"B0040HNZTW"},
    {"UserID":"A32KHS0VN0N0HB",
    "Rating":3.0,
    "ReviewTime":1496177108,
    "Review":"有特色，也比较卫生",
    "MealID":"B006Z48TZS"},{"UserID":"A1YQ4Z5U9NIGP",
    "Rating":5.0,"ReviewTime":1496177276,
    "Review":"家常美味，推荐！",
    "MealID":"B00CDBTQCW"}]
    注意这是一行，首尾有 [] 这个显然不是 json 标准的格式

    目标 json 格式:
    {"UserID":"A2WOH395IHGS0T","Rating":5.0,"ReviewTime":1496177056,"Review":"风味独特，真的不错！","MealID":"B0040HNZTW"}
    {"UserID":"A32KHS0VN0N0HB","Rating":3.0,"ReviewTime":1496177108,"Review":"有特色，也比较卫生","MealID":"B006Z48TZS"}
    同样是一行数据
    """
    with open(sourcefilepath, "r", encoding="utf-8") as fd_r:
        line_list = fd_r.readlines()

    
    json_string = line_list[0].strip("[").strip("]")
    count = json_string.count("}")
    index = json_string.index("}")
    i = 0
    line1_list = []
    with open(destfilepath, "w", encoding="utf-8") as fd_w:
        while i < count:
            line1 = json_string[0:index + 1]
            line1_list.append(line1)
            json_string = json_string[index + 2:]
            count = json_string.count("}")
            # "}" 这个字符没有，循环就要跳出，否则后面的 index = json_string.index("}") 就会抛异常
            if (count == 0):
                break
            index = json_string.index("}")
            fd_w.write(line1)
            # 注意这里不要加回车换行符，否则 spark.read.json 会多出一列:_corrupt_record:string。
            # 出现这样的错误主要是回车换行符造成的
            #fd_w.write("\n")


def transform_json1(sourcefilepath, destfilepath):
    """
    :param sourcefilepath: 原 json 文件绝对路径
    :param destfilepath: 转换后存储新 json 文件绝对的路径
    :return:
    这个函数与 transform_json 功能相同，但是速度比它快5倍左右
    """
    with open(sourcefilepath, "r", encoding="utf-8") as fd_r:
        line_list = fd_r.readlines()
    json_string = line_list[0].strip("[").strip("]")

    with open(destfilepath, "w", encoding="utf-8") as fd_w:
        for line in json_string.strip().split("}"):
            line = line.strip() + '}'
            if line.startswith(","):
                # 原文件两个{}花括号之间用 , 隔开，需要将这里的 "," 逗号字符去掉。{} 花括号内的逗号不去掉
                fd_w.write(line[1:])
            else:
                fd_w.write(line)
            # fd_w.write("\r")


def main():
    sourcefilepath = "../data/MealRatings_201705_201706.json"
    destfilepath = "../data/MealRatings.json"
    destfilepath1 = "../data/MealRatings1.json"
    starttime = datetime.datetime.now()
    transform_json(sourcefilepath, destfilepath)
    # endtime - starttime  transform_json 函数运行的时间，这样就可以大致的比较小两者的时间复杂度
    endtime = datetime.datetime.now()
    print("transform_json func elapsed time: ", (endtime - starttime).microseconds)
    time.sleep(5)
    starttime = datetime.datetime.now()
    transform_json1(sourcefilepath, destfilepath1)
    endtime = datetime.datetime.now()
    print("transform_json1 func elapsed time: ", (endtime - starttime).microseconds)


if __name__ == '__main__':
    main()